/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package main;

import simulator.EventSimulator;
import java.util.Iterator;

import com.martiansoftware.jsap.*;
import utils.Utils;

/**
 *
 * @author wpc
 */
public class Main {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws Exception {

    JSAPResult opt = getArgs(args);
    Utils.G_SIZE = opt.getLong("arrival");
    Utils.BATCH_SIZE = opt.getLong("batch");
    Utils.K = opt.getInt("queue");
    Utils.PRINT_QUEUE_DELAY = opt.getBoolean("printQueueDelay");

    System.out.println("Initialize network...");
    EventSimulator es = (new EventSimulator());
    if(opt.getBoolean("rMode")){
      es.initr1();
      Utils.ALL_BATCHES = true;
    }
    else
      es.initr2();

    System.out.println("Run Simulator...");
    es.run();
    System.out.println("Calculate Results...");
    es.display_results();
  }

  private static JSAPResult getArgs(String[] args) throws Exception
  {
    JSAP jsap = new JSAP();

    //R1 or R2
    Switch rMode = new Switch("rMode")
      .setShortFlag('1')
      .setLongFlag("r1");
    rMode.setHelp("Run single queue, or it will run network default");

    //Queue Delay or not
    Switch printQueueDelay = new Switch("printQueueDelay")
      .setShortFlag('d')
      .setLongFlag("queueDelay");
    printQueueDelay.setHelp("Print the Queue Delay in Result");

    FlaggedOption gSize = new FlaggedOption("arrival")
      .setStringParser(JSAP.LONG_PARSER)
      .setDefault(Long.toString(Utils.G_SIZE))
      .setRequired(false)
      .setShortFlag('a')
      .setLongFlag("arrival");
    gSize.setHelp("Set the Arrival Packet Size(network: size*2.45)");
    FlaggedOption bSize = new FlaggedOption("batch")
      .setStringParser(JSAP.LONG_PARSER)
      .setDefault(Long.toString(Utils.BATCH_SIZE))
      .setRequired(false)
      .setShortFlag('b')
      .setLongFlag("batch");
    bSize.setHelp("Set the Batch Size");
    FlaggedOption queueSize = new FlaggedOption("queue")
      .setStringParser(JSAP.INTEGER_PARSER)
      .setDefault(Integer.toString(Utils.K))
      .setRequired(false)
      .setShortFlag('q')
      .setLongFlag("queue");
    bSize.setHelp("Set the Queue Size");


    //help
    Switch help = new Switch("help")
      .setShortFlag('h')
      .setLongFlag("help");
    help.setHelp("Display this Useage.");

    jsap.registerParameter(rMode);
    jsap.registerParameter(printQueueDelay);
    jsap.registerParameter(help);
    jsap.registerParameter(gSize);
    jsap.registerParameter(bSize);
    jsap.registerParameter(queueSize);

    JSAPResult config = jsap.parse(args);

    //help
    boolean f = config.getBoolean("help");
    if(f)error(config,jsap);

    // check whether the command line was valid, and if it wasn't,
    // display usage information and exit.
    if (!config.success()){
      error(config, jsap);
    }

    return config;
  }


  protected static final String USAGE = "Usage: java -jar xxx.jar";

  /**
   * Error and exit
   */
  private static void error(JSAPResult config, JSAP jsap)
  {
    error("\n");
    for (Iterator errs = config.getErrorMessageIterator(); errs.hasNext();) error("Error: " + errs.next()+"\n");
    error("\n");
    error(USAGE);
    error(" "+jsap.getUsage()+"\n\n");
    error(" "+jsap.getHelp()+"\n");
    System.exit(1);
  }

  /**
   * Print the Error message and exit
   */
  private static void error(String m)
  {
    System.err.print(m);
  }
}
